'''
Created on Apr 19, 2012

@author: lavang
'''
import unittest
from Interface.administratorPage import xpathUserGroups
from Interface.userGroupsPage import xpathNewGroup, xpathTxtName,\
    xpathTxtDescription, xpathTxtUniqueGroupKey, xpathTxtMaximumKey,\
    xpathCbbDefaultLearnerType, xpathBtnSubmit, xpathLinkUserGroups, xpathDelete,\
    xpathTxtSearchGroup, xpathTabGroupUser, xpathTabGroupLesson,\
    xpathTabGroupCourse
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
fc_set='on'

# Click on User groups to access User group page
def clickOnUserGroups(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathUserGroups())
    element.click()
    
# Click on New group
def clickOnNewGroup(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathNewGroup())
    element.click()
    
# Click on a group
def ClickOnGroup(self,userGroupName):
    browser=self.browser   
    element=browser.find_element_by_xpath("//span[contains(text(),'"+userGroupName+"')]")
    element.click()
    
# Search user group
def searchUserGroup(self,userGroupName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTxtSearchGroup())
    element.clear()
    element.send_keys(userGroupName)
    element.send_keys(Keys.RETURN)
    
# Create a new User group
def createUserGroup(self,name,description,uniqueKey,maxKey,learnerType):
    #enter group's name
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTxtName())
    element.clear()
    element.send_keys(name)
    
    # enter description
    element=browser.find_element_by_xpath(xpathTxtDescription())
    element.clear()
    element.send_keys(description)
    
    # enter unique key
    element=browser.find_element_by_xpath(xpathTxtUniqueGroupKey())
    element.clear()
    element.send_keys(uniqueKey)
    
    # enter maximum key
    element=browser.find_element_by_xpath(xpathTxtMaximumKey())
    element.clear()
    element.send_keys(maxKey)
    
    # select default learner type
    element=browser.find_element_by_xpath(xpathCbbDefaultLearnerType())
    Select(element).select_by_visible_text(learnerType)
    
    # clic submit button
    element=browser.find_element_by_xpath(xpathBtnSubmit())
    element.click()
    
# back to User group page
def clickOnLinkUserGroup(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkUserGroups())
    element.click()
    
# verify that user group is created successfully
def isUserGroupCreatedSuccessfully(self,userGroupName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//span[contains(text(),'"+userGroupName+"')]"))
        return True
    except:
        return False
    
# delete a group
def deleteUserGroup(self,userGroupName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathDelete(userGroupName))
    element.click()
    alert=browser.switch_to_alert()
    alert.accept()
    browser.refresh()
    
# verify that group is deleted
def isUserGroupDeleted(self,userGroupName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//span[contains(text(),'"+userGroupName+"')]"))
        return True
    except:
        return False
    
# verify that group is searched
def isUserGroupSearch(self,userGroupName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//span[contains(text(),'"+userGroupName+"')]"))
        return True
    except:
        return False
    
# click tab group user
def clickOnTabGroupUser(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTabGroupUser())
    element.click()
    browser.implicitly_wait(30)
    
# click tab group lesson
def clickOnTabGroupLesson(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTabGroupLesson())
    element.click()
    browser.implicitly_wait(30)
    
# click tab group course
def clickOnTabGroupCourse(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTabGroupCourse())
    element.click()
    browser.implicitly_wait(30)
    
# Set member for group
def setGroupMember(self,username,set='on'):
    browser=self.browser
    element=browser.find_element_by_xpath("//input[@id='checked_"+username+"']")
    if set=='on':
        if element.is_selected():
            pass
        else:
            element.click()
    elif set=='off':
        if element.is_selected():
            element.click()
        else:
            pass
    else:
        pass
    browser.implicitly_wait(30)
    
# Set lesson for group
def setGroupLesson(self,lesson,set='on'):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'"+lesson+"')]/../../td[4]/input")
    if set=='on':
        if element.is_selected():
            pass
        else:
            element.click()
            browser.implicitly_wait(30)
    elif set=='off':
        if element.is_selected():
            element.click()
        else:
            pass
    else:
        pass
    browser.implicitly_wait(30)
    
# set Group course
def setGroupCourse(self,course,set='on'):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'"+course+"')]/../../td[4]/input")
    if set=='on':
        if element.is_selected():
            pass
        else:
            element.click()
            browser.implicitly_wait(30)
    elif set=='off':
        if element.is_selected():
            element.click()
        else:
            pass
    else:
        pass
    browser.implicitly_wait(30)
    
# Verify that member is set
def isGroupMemberSet(self,username):
    browser=self.browser
    element=browser.find_element_by_xpath("//input[@id='checked_"+username+"']")
    if fc_set=='on':
        if element.is_selected():
            return True
        else:
            return False
    else:
        if element.is_selected():
            return False
        else:
            return True

#Verify that lesson is set
def isGroupLessonSet(self,lesson):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'"+lesson+"')]/../../td[4]/input")
    if fc_set=='on':
        if element.is_selected():
            return True
        else:
            return False
    else:
        if element.is_selected():
            return False
        else:
            return True   
        
# Verify that courrse is set
def isGroupCourseSet(self,course):    
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'"+course+"')]/../../td[4]/input")
    if fc_set=='on':
        if element.is_selected():
            return True
        else:
            return False
    else:
        if element.is_selected():
            return False
        else:
            return True 